﻿@page "/Admin/Roles"
@attribute [Authorize]
@attribute [TabItemOption(Text ="角色管理",Icon ="fa fa-th")]
@inherits BasePage
@inject DialogService DialogService
@inject ISysOrgService SysOrgService
@inject ISysUserService SysUserService
@inject ISysRoleService SysRoleService

<div class="overflow-hidden h-100">
    <Table TItem="SysRole"
            @ref = "@_table"
            IsPagination="true"
            IsStriped="true"
            IsMultipleSelect="true"
            IsBordered="false"
            IsFixedHeader="true" 
            AllowResizing="true"
            ShowLoading="true"
            ShowColumnList="true"
            ShowCardView="true"
            ShowToolbar="true"
            ShowSearch="true"
            ShowExtendButtons="true" 
            ShowEmpty="true" 
            SearchModel="@_search" 
            SearchMode="SearchMode.Top"
            EditDialogItemsPerRow="1"
            EditDialogSize="Size.Medium"
            ExtendButtonColumnWidth="195"
            PageItemsSource="@(new int[] { 15, 50 })"
            OnQueryAsync="@OnQueryAsync"
            OnSaveAsync="@OnSaveAsync"
            OnDeleteAsync="@OnDeleteAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.Name" />
            <TableColumn @bind-Field="@context.RoleType"/>
            <TableColumn @bind-Field="@context.Code" />
            <TableColumn @bind-Field="@context.Sort"/>
        </TableColumns>
        <EditTemplate>
                <div class="row g-3 form-inline">
                    <div class="col-sm-12">
                        <BootstrapInput @bind-Value="@context.RoleType" maxlength="50" />
                    </div>

                    <div class="col-sm-12">
                        <BootstrapInput @bind-Value="@context.Name" maxlength="50" />
                    </div>

                    <div class="col-sm-12">
                        <BootstrapInput @bind-Value="@context.Code" maxlength="50" />
                    </div>

                    <div class="col-md-12">
                        <BootstrapInputNumber @bind-Value="@context.Sort" ShowButton="true" Max="999" Min="0" Step="5" />
                    </div>

                    <div class="col-sm-12">
                        <Textarea @bind-Value="@context.Remark" maxlength="50" />
                    </div>
                </div>
        </EditTemplate>
        <RowButtonTemplate>
            <DropdownButton Text="更多" 
                Items="@(new List<DropdownButtonItem>()
                {
                    new("授权菜单",()=> Task.CompletedTask),
                    new("授权数据",()=> Task.CompletedTask),
                })"/>
        </RowButtonTemplate>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-sm-4">
                        <RadioList @bind-Value="@context.RoleType" class="item-w0" ShowLabel="true"/>
                    </div>

                    <div class="col-sm-4">
                        <BootstrapInput @bind-Value="@context.Name" ShowLabel="true"/>
                    </div>

                    <div class="col-sm-4">
                        <BootstrapInput @bind-Value="@context.Code" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
    </Table>
</div>

@code{
    private Table<SysRole>? _table;
    private SysRole _search = new();

    private Task<QueryData<SysRole>> OnQueryAsync(QueryPageOptions options)
        => SysRoleService.PageAsync(_search.Adapt<RolePageInput>(options));

    private Task<bool> OnDeleteAsync(IEnumerable<SysRole> items) 
        => SysRoleService.DeleteAsync(items);

    private async Task<bool> OnSaveAsync(SysRole item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysRoleService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysRoleService.EditAsync(item);
                break;
        }
        return result;
    }
}